/**
 * 测试rect
 **/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include "../src/lib/rect.h"

rect_table_t* rect;

typedef struct user {
    int id;
    char name[20];
} user;

int main(int argc, char* argv[])
{
    rect = rect_create_with_size(8, 2);
    user *u = NULL;

    /* 测试存取 */
    uint i;
    rect_key_t key;
    for (i = 0; i < 32; i++) {
        u = malloc(sizeof(user));
        u->id = i;
        sprintf(u->name, "user %d", i);
        key.ord = i;
        rect_set(rect, key, u);
    }
    for (i = 0; i < 32; i++) {
        u = rect_get(rect, (rect_key_t) i);
        printf("user> id:%4d name:%s\n", u->id, u->name);
    }
    printf("capacity:%d, size:%d\n", rect_capacity(rect), rect_size(rect));

    /* 测试删除和修改 */
    for (i = 10; i < 11; i++) {
        rect_del(rect, (rect_key_t) i);
    }
    for (i = 20; i < 21; i++) {
        u = malloc(sizeof(user));
        u->id = i + 100;
        sprintf(u->name, "winner %d", i);
        rect_set(rect, (rect_key_t) i, u);
    }
    for (i = 0; i < 32; i++) {
        u = rect_get(rect, (rect_key_t) i);
        if (u == NULL) {
            printf("user of id %d not exists.\n", i);
        } else if (u->id != i) {
            printf("user> id:%4d name:%s, changed!\n", u->id, u->name);
        } else {
            printf("user> id:%4d name:%s\n", u->id, u->name);
        }
    }
    printf("capacity:%d, size:%d\n", rect_capacity(rect), rect_size(rect));
    rect_free(rect);
}
